<?php

namespace Frieza\Modules\Position\Controllers;

use Biz\service\advertisement\apiModule\CardWithPos;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\position\PositionConstInterface;
use Doraemon\tools\BlockType;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;
use Illuminate\Http\Request;


class CardWithPosController extends BaseController
{
    use BaseTrait;

    public $base_field_ext_type = CardWithPos::CARD_EXT_TYPE_BANNER;

    /**
     * @throws Exception
     */
    public function saveModule(Request $request, $baseInfoId)
    {
        $pos = $request->input('base.pos', 0);
        if ($pos <= 0)  throw new Exception('展示位置必选大于0');

        if ($this->positionId == PositionConstInterface::CIRCLE_CARD) {
            if ($pos > 10)  throw new Exception('展示位置必选小于等于10');
            $link = $request->input('base.link', []);
            if (empty($link['link_params']['id'])) throw new Exception('noteID为必填项');
        } else {
            if ($this->base_field_pos && ($this->base_field_pos != $pos)) throw new Exception('展示位置不允许变更');

            $ext_type = $request->input("base.ext_type", "");
            if ($ext_type == CardWithPos::CARD_EXT_TYPE_BANNER) {
                $base = $request->input('base', []);
                $rules = $request->input('rules', []);
                $pic_id = $request->input("base.pic_id", "");
                if (empty($pic_id)) throw new Exception('图片必须上传');
                $id = $base['id'] ?? 0;
                $copy_id = $base['copy_id'] ?? 0;
                $img_size = $rules['img_size'] ?? 0;
                if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) {
                    $this->rulesValid($rules);
                }
            }
        }

    }

    public function extendList(Grid $grid)
    {
        $grid->column('pos', '展示位置')->label($this->labelColor());
        $grid->column('card_info', '卡片信息')->display(function ($title, Grid\Column $column) {
            $display = [CardWithPos::CARD_EXT_MAP[$this->ext_type] ?? CardWithPos::CARD_EXT_MAP[CardWithPos::CARD_EXT_TYPE_BANNER]];
            if ($this->ext_type == CardWithPos::CARD_EXT_TYPE_CONTENT) {
                $display[] = "内容类型:" . BlockType::name($this->obj_type);
                $display[] = "内容id:" . $this->obj_id;
            } else {
                $images[] = "<img src='{$this->pic_url}' style=\"max-width:200px;max-height:200px\" class=\"img img-thumbnail\"/>";
                $display[] = implode('<br/><br/>', $images);
            }

            return implode('<br/>', $display);
        });
    }

    public function beforeTitleForm(Form $form)
    {
        if ($this->relation)
        {
            $form->text('base.pos', '展示位置')->width(50)->readonly();
        }
        else {
            if ($this->positionId == PositionConstInterface::CIRCLE_CARD)
            {
                $form->number('base.pos', '展示位置')->min(1)->max(10)->help('仅支持【1-10】');
            }
            elseif ($this->positionId == 120)
            {
                $help = [
                    "资源位只在推荐线第一页，可选1-20任意其他数字；",
                ];
                $form->number('base.pos', '展示位置')->placeholder('位置')->required()->help("<span style='color: red'>" . implode('<br/>', $help) . "</span>");
            }
            else
            {
                $help = [
                    "仅开放首个位置",
                ];
                $form->text('base.pos', '展示位置')->width(50)->placeholder('位置')->required()->help("<span style='color: red'>" . implode('<br/>', $help) . "</span>")->readonly();
            }
        }
    }

    public function beforeLinkForm(Form $form)
    {
        $allowLink = [
            LinkConf::LINK_APP_NOTE_DETAIL,
        ];
        if ($this->positionId == 120)
        {
            $allowLink = [
                LinkConf::LINK_PHOTO,
                LinkConf::LINK_ARTICLE,
                LinkConf::LINK_GUIDE,
                LinkConf::LINK_BLANK,
                LinkConf::LINK_TOPIC_DETAIL,
                LinkConf::LINK_INSPIRATION_SET,
                LinkConf::MALL_WIKI_DETAIL,
                LinkConf::LINK_USER,
                LinkConf::LINK_APP_WIKI_LIST_DETAIL,
            ];
        }
        if ($this->positionId == PositionConstInterface::CIRCLE_CARD) {
            $form->multiLinkSelect('base.link', '跳转链接', [LinkConf::LINK_APP_NOTE_DETAIL]);
            $form->hidden('base.ext_type');
        }
        else
        {
            $form->radio('base.ext_type', '卡片类型')
                ->options(CardWithPos::CARD_EXT_MAP)
                ->when(CardWithPos::CARD_EXT_TYPE_BANNER, function (Form $form) {
                    $this->bannerFormWithCheck($form);
                    $form->multiLinkSelect('base.link', 'Banner跳转链接');
                })->when(CardWithPos::CARD_EXT_TYPE_CONTENT, function (Form $form) use ($allowLink) {
                    $form->multiLinkSelect('base.link', '内容', $allowLink)->help('支持note');
                });
        }
    }

    public function defaultBaseInfo(Request $request): array
    {
        if ($this->positionId == PositionConstInterface::CIRCLE_CARD) {
            $this->base_field_ext_type = CardWithPos::CARD_EXT_TYPE_CONTENT;
        }
        $response = parent::defaultBaseInfo($request);

        $response['pos'] = 1;
        if ($this->positionId == PositionConstInterface::CIRCLE_CARD) {
            $response['link']['link_type'] = LinkConf::LINK_APP_NOTE_DETAIL;
        }
        return $response;
    }
}

